var form, $,areaData;
layui.config({
    base : "/js/"
}).extend({
    "address" : "address"
});
layui.use(['form','layer','upload','laydate',"address"],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        address = layui.address;

    //回显用户个人信息
    var _ticket = $.cookie("token");
    if(_ticket != null && _ticket !== ""){
        $.ajax({
            url : "/user/token/" + _ticket,//单点登录链接
            dataType : "jsonp",
            type : "GET",
            success : function(data){
                if (data.code == 200) {
                    var user = data.data.user;
                    $("#id").val(user.id);
                    $("#uploadImage").attr("src",user.pic);
                    $("#photo").val(user.pic);
                    $("#username").val(user.name);
                    $("#sex input[value="+ user.sex +"]").val(user.sex);
                    $("#mobile").val(user.mobile);
                    $("#email").val(user.email);
                } else {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
                    window.location.href = "/views/login.html";
                }
            }
        });
    } else {
        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
        window.location.href = "/views/login.html";
    }

    //上传头像
    upload.render({
        elem: '.userFaceBtn',
        field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
        url: '/upload/pic',
        done: function(res, index, upload){
            if (res.code === 0) {
                layer.msg(res.msg);
                $("#userFace").attr("src",res.url);
                $("#photo").val(res.url);
            } else {
                layer.msg(res.msg);
            }
        }
    });

    //获取省信息
    address.provinces();

    //提交个人资料
    form.on("submit(changeUser)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //更新数据库
        $.ajax({
            url: '/user/'+data.id,
            data: data.field,
            type: "put",
            success: function(data) {
                if(data.code == 200) {
                    layer.close(index);
                    layer.msg("修改成功！");
                    location.reload();
                }else {
                    layer.close(index);
                    layer.msg("修改失败！请重试");
                    location.reload();
                }
            },
            error:function() {
                layer.close(index);
                layer.msg("无法连接服务器，请重试");
            }
        });
        //更新redis缓存数据
        _ticket = $.cookie("token");
        if(_ticket != null && _ticket !== ""){
            $.ajax({
                url : "/user/token/edit?token=" + _ticket,//单点登录链接
                dataType : "jsonp",
                data : data.field,
                type : "POST",
                success : function(data){
                    if (data.code == 200) {
                        var user = data.data.user;

                        $("#uploadFile").attr("src",user.pic);
                        $("#photo").val(user.pic);
                        $("#username").val(user.name);
                        $("#sex input[value="+ user.sex +"]").val(user.sex);
                        $("#mobile").val(user.mobile);
                        $("#email").val(user.email);
                    } else {
                        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
                        window.location.href = "/views/login.html";
                    }
                }
            });
        } else {
            layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
            window.location.href = "/views/login.html";
        }
        return false;
    });

    //修改密码
    form.on("submit(changePwd)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            layer.msg("密码修改成功！");
            $(".pwd").val('');
        },2000);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
});